class Solution {
    public String countAndSay(int n) {
        String ret = "1";
        for(int i = 0; i < n - 1; i++) {
            StringBuilder sb = new StringBuilder();
            int left = 0, right = 0;
            int len = ret.length();
            while(right < len) {
                while(right < len && ret.charAt(right) == ret.charAt(left)) right++;
            sb.append(right - left);
            sb.append(ret.charAt(left));
            left = right;
            }
            ret = sb.toString();
        }
        return ret;
    }
}